<%@tag import="java.sql.Connection"%>
<%@tag import="by.epam.communalservices.dao.jdbc.JdbcDaoJkhWorker"%>
<%@tag import="by.epam.communalservices.entity.JkhWorker"%>
<%@ attribute name="id" required="true"%>
<%@ attribute name="name" required="true"%>
<%@ attribute name="surname" required="true"%>
<%@ attribute name="patronymic" required="true"%>
<%@ attribute name="specialty" required="true"%>
<%@ attribute name="rate" required="true"%>
<%@ attribute name="delete" required="true"%>
<%@ attribute name="submit" required="true"%>

<%
    request.setAttribute("message", "");

    Connection con = (Connection) request.getSession().getAttribute("currentConnection");
    JkhWorker jkhWorker = null;
    JdbcDaoJkhWorker daoWorker = new JdbcDaoJkhWorker(con);

    try {
        if (getJspContext().getAttribute("id") != ""
                && Long.parseLong(getJspContext().getAttribute("id").toString()) > 0) {
            jkhWorker = daoWorker.read(Long.parseLong(getJspContext().getAttribute("id").toString()));
            if (getJspContext().getAttribute("submit") != "") {
                jkhWorker.setName(getJspContext().getAttribute("name").toString());
                jkhWorker.setSurname(getJspContext().getAttribute("surname").toString());
                jkhWorker.setPatronymic(getJspContext().getAttribute("patronymic").toString());
                jkhWorker.setSpecialty(getJspContext().getAttribute("specialty").toString());
                jkhWorker.setRate(Integer.parseInt(getJspContext().getAttribute("rate").toString()));
                daoWorker.update(jkhWorker.getId(), jkhWorker);
                request.setAttribute("message", "Worker updated");
            }
        } else if (getJspContext().getAttribute("delete") != ""
                && Long.parseLong(getJspContext().getAttribute("delete").toString()) > 0) {
            daoWorker.delete(Long.parseLong(getJspContext().getAttribute("delete").toString()));
            request.setAttribute("message", "Worker deleted");
        } else {
            jkhWorker = new JkhWorker();
            if (getJspContext().getAttribute("submit") != "") {
                jkhWorker.setName(getJspContext().getAttribute("name").toString());
                jkhWorker.setSurname(getJspContext().getAttribute("surname").toString());
                jkhWorker.setPatronymic(getJspContext().getAttribute("patronymic").toString());
                jkhWorker.setSpecialty(getJspContext().getAttribute("specialty").toString());
                try {
                    jkhWorker.setRate(Integer.parseInt(getJspContext().getAttribute("rate").toString()));
                } catch (Exception e) {
                    jkhWorker.setRate(null);
                }
                jkhWorker.setId(daoWorker.create(jkhWorker));
                request.setAttribute("message", "Worker Created");
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
        out.println(e.getMessage() + "<br>");
        for (StackTraceElement el : e.getStackTrace()) {
            out.println(el.toString() + "<br>");
        }
    }
    if (jkhWorker == null) {
        request.setAttribute("form", false);
        if (getJspContext().getAttribute("delete") == "") {
            request.setAttribute("message", "Worker Not Found");
        }
    } else {
        request.setAttribute("form", true);
        request.setAttribute("worker", jkhWorker);
    }
    
%>